In [7]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03617480_2967.JPEG
Augmentation 1: Predicted label = poncho, True label = kimono
Augmentation 2: Predicted label = poncho, True label = kimono
Augmentation 3: Predicted label = poncho, True label = kimono
Augmentation 4: Predicted label = poncho, True label = kimono
Augmentation 5: Predicted label = poncho, True label = kimono
Augmentation 6: Predicted label = poncho, True label = kimono
Augmentation 7: Predicted label = poncho, True label = kimono
Augmentation 8: Predicted label = poncho, True label = kimono
Augmentation 9: Predicted label = poncho, True label = kimono
Augmentation 10: Predicted label = poncho, True label = kimono
Augmentation 11: Predicted label = poncho, True label = kimono
Augmentation 12: Predicted label = poncho, True label = kimono
Augmentation 13: Predicted label = poncho, True label = kimono
Augmentation 14: Predicted label = poncho, True label = kimono
Augmentation 15: Predicted label = poncho, True label = kimono
Augmentation 16: Predicted label = kimono, True label = kimono
Augmentation 17: Predicted label = kimono, True label = kimono
Augmentation 18: Predicted label = kimono, True label = kimono
Augmentation 19: Predicted label = kimono, True label = kimono
Augmentation 20: Predicted label = kimono, True label = kimono
Augmentation 21: Predicted label = kimono, True label = kimono
Augmentation 22: Predicted label = vestment, True label = kimono
Augmentation 23: Predicted label = vestment, True label = kimono
Augmentation 24: Predicted label = vestment, True label = kimono
Augmentation 25: Predicted label = vestment, True label = kimono
Augmentation 26: Predicted label = vestment, True label = kimono
Augmentation 27: Predicted label = vestment, True label = kimono
Augmentation 28: Predicted label = cloak, True label = kimono
Augmentation 29: Predicted label = vestment, True label = kimono
Augmentation 30: Predicted label = trench coat, True label = kimono
Augmentation 31: Predicted label = cloak, True label = kimono
Augmentation 32: Predicted label = cloak, True label = kimono
Augmentation 33: Predicted label = trench coat, True label = kimono
Augmentation 34: Predicted label = trench coat, True label = kimono
Augmentation 35: Predicted label = cloak, True label = kimono
Augmentation 36: Predicted label = trench coat, True label = kimono
Augmentation 37: Predicted label = trench coat, True label = kimono
Augmentation 38: Predicted label = trench coat, True label = kimono
Augmentation 39: Predicted label = trench coat, True label = kimono
Augmentation 40: Predicted label = trench coat, True label = kimono
Augmentation 41: Predicted label = trench coat, True label = kimono
Augmentation 42: Predicted label = hoopskirt, crinoline, True label = kimono
Augmentation 43: Predicted label = trench coat, True label = kimono
Augmentation 44: Predicted label = hoopskirt, crinoline, True label = kimono
Augmentation 45: Predicted label = trench coat, True label = kimono
Augmentation 46: Predicted label = trench coat, True label = kimono
Augmentation 47: Predicted label = trench coat, True label = kimono
Augmentation 48: Predicted label = trench coat, True label = kimono
Augmentation 49: Predicted label = trench coat, True label = kimono
Augmentation 50: Predicted label = hoopskirt, crinoline, True label = kimono
Overall Accuracy on Augmented Images: 12.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[7], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [12]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03791053_24291.JPEG
Augmentation 1: Predicted label = moped, True label = motor scooter, scooter
Augmentation 2: Predicted label = moped, True label = motor scooter, scooter
Augmentation 3: Predicted label = moped, True label = motor scooter, scooter
Augmentation 4: Predicted label = moped, True label = motor scooter, scooter
Augmentation 5: Predicted label = moped, True label = motor scooter, scooter
Augmentation 6: Predicted label = moped, True label = motor scooter, scooter
Augmentation 7: Predicted label = moped, True label = motor scooter, scooter
Augmentation 8: Predicted label = moped, True label = motor scooter, scooter
Augmentation 9: Predicted label = moped, True label = motor scooter, scooter
Augmentation 10: Predicted label = moped, True label = motor scooter, scooter
Augmentation 11: Predicted label = moped, True label = motor scooter, scooter
Augmentation 12: Predicted label = moped, True label = motor scooter, scooter
Augmentation 13: Predicted label = moped, True label = motor scooter, scooter
Augmentation 14: Predicted label = moped, True label = motor scooter, scooter
Augmentation 15: Predicted label = moped, True label = motor scooter, scooter
Augmentation 16: Predicted label = moped, True label = motor scooter, scooter
Augmentation 17: Predicted label = moped, True label = motor scooter, scooter
Augmentation 18: Predicted label = moped, True label = motor scooter, scooter
Augmentation 19: Predicted label = moped, True label = motor scooter, scooter
Augmentation 20: Predicted label = moped, True label = motor scooter, scooter
Augmentation 21: Predicted label = moped, True label = motor scooter, scooter
Augmentation 22: Predicted label = moped, True label = motor scooter, scooter
Augmentation 23: Predicted label = moped, True label = motor scooter, scooter
Augmentation 24: Predicted label = moped, True label = motor scooter, scooter
Augmentation 25: Predicted label = moped, True label = motor scooter, scooter
Augmentation 26: Predicted label = moped, True label = motor scooter, scooter
Augmentation 27: Predicted label = moped, True label = motor scooter, scooter
Augmentation 28: Predicted label = moped, True label = motor scooter, scooter
Augmentation 29: Predicted label = moped, True label = motor scooter, scooter
Augmentation 30: Predicted label = moped, True label = motor scooter, scooter
Augmentation 31: Predicted label = moped, True label = motor scooter, scooter
Augmentation 32: Predicted label = moped, True label = motor scooter, scooter
Augmentation 33: Predicted label = moped, True label = motor scooter, scooter
Augmentation 34: Predicted label = moped, True label = motor scooter, scooter
Augmentation 35: Predicted label = moped, True label = motor scooter, scooter
Augmentation 36: Predicted label = moped, True label = motor scooter, scooter
Augmentation 37: Predicted label = moped, True label = motor scooter, scooter
Augmentation 38: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 39: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 40: Predicted label = moped, True label = motor scooter, scooter
Augmentation 41: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 42: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 43: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 44: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 45: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 46: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 47: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 48: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 49: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Augmentation 50: Predicted label = motor scooter, scooter, True label = motor scooter, scooter
Overall Accuracy on Augmented Images: 24.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[12], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [41]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03950228_20716.JPEG
Augmentation 1: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 2: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 3: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 4: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 5: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 6: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 7: Predicted label = pitcher, ewer, True label = pitcher, ewer
Augmentation 8: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 9: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 10: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 11: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 12: Predicted label = pitcher, ewer, True label = pitcher, ewer
Augmentation 13: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 14: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 15: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 16: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 17: Predicted label = pitcher, ewer, True label = pitcher, ewer
Augmentation 18: Predicted label = pitcher, ewer, True label = pitcher, ewer
Augmentation 19: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 20: Predicted label = coffeepot, True label = pitcher, ewer
Augmentation 21: Predicted label = pitcher, ewer, True label = pitcher, ewer
Augmentation 22: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 23: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 24: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 25: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 26: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 27: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 28: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 29: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 30: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 31: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 32: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 33: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 34: Predicted label = pop bottle, soda bottle, True label = pitcher, ewer
Augmentation 35: Predicted label = beer glass, True label = pitcher, ewer
Augmentation 36: Predicted label = pop bottle, soda bottle, True label = pitcher, ewer
Augmentation 37: Predicted label = pop bottle, soda bottle, True label = pitcher, ewer
Augmentation 38: Predicted label = pop bottle, soda bottle, True label = pitcher, ewer
Augmentation 39: Predicted label = pop bottle, soda bottle, True label = pitcher, ewer
Augmentation 40: Predicted label = beaker, True label = pitcher, ewer
Augmentation 41: Predicted label = safety pin, True label = pitcher, ewer
Augmentation 42: Predicted label = safety pin, True label = pitcher, ewer
Augmentation 43: Predicted label = jean, blue jean, denim, True label = pitcher, ewer
Augmentation 44: Predicted label = manhole cover, True label = pitcher, ewer
Augmentation 45: Predicted label = jean, blue jean, denim, True label = pitcher, ewer
Augmentation 46: Predicted label = safety pin, True label = pitcher, ewer
Augmentation 47: Predicted label = safety pin, True label = pitcher, ewer
Augmentation 48: Predicted label = jean, blue jean, denim, True label = pitcher, ewer
Augmentation 49: Predicted label = manhole cover, True label = pitcher, ewer
Augmentation 50: Predicted label = safety pin, True label = pitcher, ewer
Overall Accuracy on Augmented Images: 10.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[41], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [62]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n07749582_19627.JPEG
Augmentation 1: Predicted label = orange, True label = lemon
Augmentation 2: Predicted label = orange, True label = lemon
Augmentation 3: Predicted label = orange, True label = lemon
Augmentation 4: Predicted label = orange, True label = lemon
Augmentation 5: Predicted label = orange, True label = lemon
Augmentation 6: Predicted label = orange, True label = lemon
Augmentation 7: Predicted label = orange, True label = lemon
Augmentation 8: Predicted label = orange, True label = lemon
Augmentation 9: Predicted label = orange, True label = lemon
Augmentation 10: Predicted label = orange, True label = lemon
Augmentation 11: Predicted label = orange, True label = lemon
Augmentation 12: Predicted label = orange, True label = lemon
Augmentation 13: Predicted label = orange, True label = lemon
Augmentation 14: Predicted label = orange, True label = lemon
Augmentation 15: Predicted label = orange, True label = lemon
Augmentation 16: Predicted label = orange, True label = lemon
Augmentation 17: Predicted label = orange, True label = lemon
Augmentation 18: Predicted label = orange, True label = lemon
Augmentation 19: Predicted label = orange, True label = lemon
Augmentation 20: Predicted label = orange, True label = lemon
Augmentation 21: Predicted label = orange, True label = lemon
Augmentation 22: Predicted label = orange, True label = lemon
Augmentation 23: Predicted label = orange, True label = lemon
Augmentation 24: Predicted label = lemon, True label = lemon
Augmentation 25: Predicted label = orange, True label = lemon
Augmentation 26: Predicted label = orange, True label = lemon
Augmentation 27: Predicted label = orange, True label = lemon
Augmentation 28: Predicted label = lemon, True label = lemon
Augmentation 29: Predicted label = lemon, True label = lemon
Augmentation 30: Predicted label = lemon, True label = lemon
Augmentation 31: Predicted label = lemon, True label = lemon
Augmentation 32: Predicted label = lemon, True label = lemon
Augmentation 33: Predicted label = lemon, True label = lemon
Augmentation 34: Predicted label = lemon, True label = lemon
Augmentation 35: Predicted label = lemon, True label = lemon
Augmentation 36: Predicted label = wool, woolen, woollen, True label = lemon
Augmentation 37: Predicted label = orange, True label = lemon
Augmentation 38: Predicted label = wool, woolen, woollen, True label = lemon
Augmentation 39: Predicted label = banana, True label = lemon
Augmentation 40: Predicted label = wool, woolen, woollen, True label = lemon
Augmentation 41: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 42: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 43: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 44: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 45: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 46: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 47: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 48: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 49: Predicted label = computer keyboard, keypad, True label = lemon
Augmentation 50: Predicted label = computer keyboard, keypad, True label = lemon
Overall Accuracy on Augmented Images: 18.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[62], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [64]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03868863_1727.JPEG
Augmentation 1: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 2: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 3: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 4: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 5: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 6: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 7: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 8: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 9: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 10: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 11: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 12: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 13: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 14: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 15: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 16: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 17: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 18: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 19: Predicted label = gasmask, respirator, gas helmet, True label = oxygen mask
Augmentation 20: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 21: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 22: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 23: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 24: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 25: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 26: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 27: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 28: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 29: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 30: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 31: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 32: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 33: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 34: Predicted label = football helmet, True label = oxygen mask
Augmentation 35: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 36: Predicted label = oxygen mask, True label = oxygen mask
Augmentation 37: Predicted label = football helmet, True label = oxygen mask
Augmentation 38: Predicted label = crash helmet, True label = oxygen mask
Augmentation 39: Predicted label = crash helmet, True label = oxygen mask
Augmentation 40: Predicted label = crash helmet, True label = oxygen mask
Augmentation 41: Predicted label = crash helmet, True label = oxygen mask
Augmentation 42: Predicted label = crash helmet, True label = oxygen mask
Augmentation 43: Predicted label = crash helmet, True label = oxygen mask
Augmentation 44: Predicted label = iron, smoothing iron, True label = oxygen mask
Augmentation 45: Predicted label = crash helmet, True label = oxygen mask
Augmentation 46: Predicted label = crash helmet, True label = oxygen mask
Augmentation 47: Predicted label = crash helmet, True label = oxygen mask
Augmentation 48: Predicted label = crash helmet, True label = oxygen mask
Augmentation 49: Predicted label = crash helmet, True label = oxygen mask
Augmentation 50: Predicted label = electric fan, blower, True label = oxygen mask
Overall Accuracy on Augmented Images: 32.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[64], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [68]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03777754_2812.JPEG
Augmentation 1: Predicted label = scale, weighing machine, True label = modem
Augmentation 2: Predicted label = scale, weighing machine, True label = modem
Augmentation 3: Predicted label = scale, weighing machine, True label = modem
Augmentation 4: Predicted label = scale, weighing machine, True label = modem
Augmentation 5: Predicted label = scale, weighing machine, True label = modem
Augmentation 6: Predicted label = scale, weighing machine, True label = modem
Augmentation 7: Predicted label = scale, weighing machine, True label = modem
Augmentation 8: Predicted label = scale, weighing machine, True label = modem
Augmentation 9: Predicted label = scale, weighing machine, True label = modem
Augmentation 10: Predicted label = scale, weighing machine, True label = modem
Augmentation 11: Predicted label = scale, weighing machine, True label = modem
Augmentation 12: Predicted label = scale, weighing machine, True label = modem
Augmentation 13: Predicted label = scale, weighing machine, True label = modem
Augmentation 14: Predicted label = scale, weighing machine, True label = modem
Augmentation 15: Predicted label = scale, weighing machine, True label = modem
Augmentation 16: Predicted label = scale, weighing machine, True label = modem
Augmentation 17: Predicted label = scale, weighing machine, True label = modem
Augmentation 18: Predicted label = scale, weighing machine, True label = modem
Augmentation 19: Predicted label = scale, weighing machine, True label = modem
Augmentation 20: Predicted label = scale, weighing machine, True label = modem
Augmentation 21: Predicted label = scale, weighing machine, True label = modem
Augmentation 22: Predicted label = modem, True label = modem
Augmentation 23: Predicted label = modem, True label = modem
Augmentation 24: Predicted label = modem, True label = modem
Augmentation 25: Predicted label = modem, True label = modem
Augmentation 26: Predicted label = modem, True label = modem
Augmentation 27: Predicted label = modem, True label = modem
Augmentation 28: Predicted label = modem, True label = modem
Augmentation 29: Predicted label = modem, True label = modem
Augmentation 30: Predicted label = modem, True label = modem
Augmentation 31: Predicted label = modem, True label = modem
Augmentation 32: Predicted label = modem, True label = modem
Augmentation 33: Predicted label = modem, True label = modem
Augmentation 34: Predicted label = modem, True label = modem
Augmentation 35: Predicted label = modem, True label = modem
Augmentation 36: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 37: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 38: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 39: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 40: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 41: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 42: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 43: Predicted label = stingray, True label = modem
Augmentation 44: Predicted label = stingray, True label = modem
Augmentation 45: Predicted label = stingray, True label = modem
Augmentation 46: Predicted label = computer keyboard, keypad, True label = modem
Augmentation 47: Predicted label = stingray, True label = modem
Augmentation 48: Predicted label = stingray, True label = modem
Augmentation 49: Predicted label = stingray, True label = modem
Augmentation 50: Predicted label = eel, True label = modem
Overall Accuracy on Augmented Images: 28.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[68], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [79]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03691459_57535.JPEG
Augmentation 1: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 2: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 3: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 4: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 5: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 6: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 7: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 8: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 9: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 10: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 11: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 12: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 13: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 14: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 15: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 16: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 17: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 18: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 19: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 20: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 21: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 22: Predicted label = desktop computer, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 23: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 24: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 25: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 26: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 27: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 28: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 29: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 30: Predicted label = tape player, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 31: Predicted label = tape player, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 32: Predicted label = tape player, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 33: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 34: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 35: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 36: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 37: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 38: Predicted label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 39: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 40: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 41: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 42: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 43: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 44: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 45: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 46: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 47: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 48: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 49: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Augmentation 50: Predicted label = home theater, home theatre, True label = loudspeaker, speaker, speaker unit, loudspeaker system, speaker system
Overall Accuracy on Augmented Images: 34.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[79], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [116]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03877845_2771.JPEG
Augmentation 1: Predicted label = monastery, True label = palace
Augmentation 2: Predicted label = monastery, True label = palace
Augmentation 3: Predicted label = monastery, True label = palace
Augmentation 4: Predicted label = monastery, True label = palace
Augmentation 5: Predicted label = monastery, True label = palace
Augmentation 6: Predicted label = monastery, True label = palace
Augmentation 7: Predicted label = monastery, True label = palace
Augmentation 8: Predicted label = monastery, True label = palace
Augmentation 9: Predicted label = monastery, True label = palace
Augmentation 10: Predicted label = monastery, True label = palace
Augmentation 11: Predicted label = monastery, True label = palace
Augmentation 12: Predicted label = monastery, True label = palace
Augmentation 13: Predicted label = monastery, True label = palace
Augmentation 14: Predicted label = monastery, True label = palace
Augmentation 15: Predicted label = monastery, True label = palace
Augmentation 16: Predicted label = monastery, True label = palace
Augmentation 17: Predicted label = monastery, True label = palace
Augmentation 18: Predicted label = monastery, True label = palace
Augmentation 19: Predicted label = monastery, True label = palace
Augmentation 20: Predicted label = monastery, True label = palace
Augmentation 21: Predicted label = monastery, True label = palace
Augmentation 22: Predicted label = monastery, True label = palace
Augmentation 23: Predicted label = monastery, True label = palace
Augmentation 24: Predicted label = monastery, True label = palace
Augmentation 25: Predicted label = monastery, True label = palace
Augmentation 26: Predicted label = palace, True label = palace
Augmentation 27: Predicted label = palace, True label = palace
Augmentation 28: Predicted label = palace, True label = palace
Augmentation 29: Predicted label = monastery, True label = palace
Augmentation 30: Predicted label = palace, True label = palace
Augmentation 31: Predicted label = palace, True label = palace
Augmentation 32: Predicted label = palace, True label = palace
Augmentation 33: Predicted label = palace, True label = palace
Augmentation 34: Predicted label = palace, True label = palace
Augmentation 35: Predicted label = palace, True label = palace
Augmentation 36: Predicted label = palace, True label = palace
Augmentation 37: Predicted label = palace, True label = palace
Augmentation 38: Predicted label = palace, True label = palace
Augmentation 39: Predicted label = palace, True label = palace
Augmentation 40: Predicted label = monastery, True label = palace
Augmentation 41: Predicted label = palace, True label = palace
Augmentation 42: Predicted label = park bench, True label = palace
Augmentation 43: Predicted label = palace, True label = palace
Augmentation 44: Predicted label = park bench, True label = palace
Augmentation 45: Predicted label = park bench, True label = palace
Augmentation 46: Predicted label = park bench, True label = palace
Augmentation 47: Predicted label = submarine, pigboat, sub, U-boat, True label = palace
Augmentation 48: Predicted label = park bench, True label = palace
Augmentation 49: Predicted label = park bench, True label = palace
Augmentation 50: Predicted label = submarine, pigboat, sub, U-boat, True label = palace
Overall Accuracy on Augmented Images: 30.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[116], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [121]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n02808440_2380.JPEG
Augmentation 1: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 2: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 3: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 4: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 5: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 6: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 7: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 8: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 9: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 10: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 11: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 12: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 13: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 14: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 15: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 16: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 17: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 18: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 19: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 20: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 21: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 22: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 23: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 24: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 25: Predicted label = tub, vat, True label = bathtub, bathing tub, bath, tub
Augmentation 26: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 27: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 28: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 29: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 30: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 31: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 32: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 33: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 34: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 35: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 36: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 37: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 38: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 39: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 40: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 41: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 42: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 43: Predicted label = washbasin, handbasin, washbowl, lavabo, wash-hand basin, True label = bathtub, bathing tub, bath, tub
Augmentation 44: Predicted label = washbasin, handbasin, washbowl, lavabo, wash-hand basin, True label = bathtub, bathing tub, bath, tub
Augmentation 45: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 46: Predicted label = washbasin, handbasin, washbowl, lavabo, wash-hand basin, True label = bathtub, bathing tub, bath, tub
Augmentation 47: Predicted label = washbasin, handbasin, washbowl, lavabo, wash-hand basin, True label = bathtub, bathing tub, bath, tub
Augmentation 48: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Augmentation 49: Predicted label = washbasin, handbasin, washbowl, lavabo, wash-hand basin, True label = bathtub, bathing tub, bath, tub
Augmentation 50: Predicted label = bathtub, bathing tub, bath, tub, True label = bathtub, bathing tub, bath, tub
Overall Accuracy on Augmented Images: 42.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[121], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]:
 
In [126]:
import os
import random
import numpy as np
import torch
from torchvision import models, transforms as T
from PIL import Image
from matplotlib import pyplot as plt

# Function to add Gaussian noise to an image with specific parameters
def augment_image_gaussian(image, var, sigma_limit, distortion_level):
    width, height = image.size
    sigma = np.sqrt(distortion_level * var)
    sigma = min(sigma, sigma_limit)
    noise = np.random.normal(0, sigma, (height, width, 3))
    noisy_image = np.array(image) + noise
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

# Function to load synset mappings
def load_synset_mapping(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    mapping = {}
    ordered_synsets = []
    for line in lines:
        parts = line.strip().split(' ', 1)
        mapping[parts[0]] = parts[1]
        ordered_synsets.append(parts[0])
    return mapping, ordered_synsets

# Function to pick a random image from ImageNet
def pick_random_image_from_imagenet(imagenet_base_directory):
    dataset_path = os.path.join(imagenet_base_directory)
    category_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
    if not category_dirs:
        raise ValueError(f"No category directories found in {dataset_path}. Please check the path.")
    
    chosen_category = random.choice(category_dirs)
    category_path = os.path.join(dataset_path, chosen_category)
    all_images = []
    for root, dirs, files in os.walk(category_path):
        all_images.extend([os.path.join(root, f) for f in files if f.lower().endswith('.jpeg')])
    
    if not all_images:
        raise ValueError(f"No images found in {category_path}.")
    
    chosen_image = random.choice(all_images)
    label = synset_to_label.get(chosen_category, "Unknown")
    return chosen_image, label

# Function to get the predicted label from the model
def get_predicted_label(model, image_tensor):
    with torch.no_grad():
        output = model(image_tensor)
    predicted_index = torch.argmax(output).item()
    predicted_synset = ordered_synsets[predicted_index]
    return synset_to_label.get(predicted_synset, "Unknown")

# Function to apply augmentations and evaluate
def apply_and_evaluate_augmentations(image, model, true_label, var, sigma_limit, distortion_level, increment_factor, num_augmentations=50):
    augmentations = []
    predicted_labels = []
    classification_correctness = []  # This list will hold the classification correctness

    for i in range(num_augmentations):  # Number of augmentations
        noisy_image_np = augment_image_gaussian(image, var, sigma_limit, distortion_level)
        noisy_image = Image.fromarray(noisy_image_np)
        noisy_image_tensor = transform(noisy_image).unsqueeze(0)
        predicted_label = get_predicted_label(model, noisy_image_tensor)

        augmentations.append(noisy_image)
        predicted_labels.append(predicted_label)

        # Debugging: Print the predicted and true labels
        print(f"Augmentation {i+1}: Predicted label = {predicted_label}, True label = {true_label}")

        # Check if the predicted label is correct
        if predicted_label == true_label:
            classification_correctness.append(1)  # Correct
        else:
            classification_correctness.append(0)  # Incorrect

        distortion_level *= increment_factor  # Increase the distortion level for the next image

    total_correct = sum(classification_correctness)
    total_augmentations = len(classification_correctness)
    total_accuracy = total_correct / total_augmentations * 100  # Convert to percentage

    return augmentations, predicted_labels, total_accuracy

# Function to create and display montage of images
def create_and_display_montage(images, titles, montage_shape):
    # Calculate the required number of rows and columns to display all images
    num_images = len(images)
    num_columns = montage_shape[1]
    num_rows = (num_images + num_columns - 1) // num_columns  # Ceiling division

    fig, axarr = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(20, num_rows * 2))
    plt.subplots_adjust(hspace=0.4, wspace=0.4)
    for i, ax in enumerate(axarr.ravel()):
        if i < num_images:
            ax.imshow(np.asarray(images[i]))
            ax.set_title(titles[i], fontsize=8)
            ax.axis('off')
        else:
            ax.axis('off')  # Hide unused subplots
    plt.show()

# Load synset mappings
synset_mapping_file = 'E:/train/LOC_synset_mapping.txt'
synset_to_label, ordered_synsets = load_synset_mapping(synset_mapping_file)

# Load the pre-trained model
model = models.resnet50(pretrained=True)
model.eval()

# Transform to apply to each image
transform = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Parameters for Gaussian noise augmentation
var = 150  # Variance for the Gaussian noise
sigma_limit = 100  # Maximum limit for the sigma value
distortion_level = 0.001  # Starting value
increment_factor = 1.3  # Increment factor for the distortion level



# Main loop to find an incorrectly classified image
classification_correctness = []
while True:
    image_path, true_label = pick_random_image_from_imagenet('E:/train')
    image = Image.open(image_path).convert('RGB')
    image_tensor = transform(image).unsqueeze(0)
    predicted_label = get_predicted_label(model, image_tensor)
    
    if predicted_label != true_label:
        print(f"Incorrectly classified image: {os.path.basename(image_path)}")
        augmented_images, predicted_labels, total_accuracy = apply_and_evaluate_augmentations(
            image, model, true_label, var, sigma_limit, distortion_level, increment_factor
        )
        print(f"Overall Accuracy on Augmented Images: {total_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

        # Plot severity of augmentation vs. classification correctness
        plt.figure(figsize=(10, 5))
        num_augmentations = len(classification_correctness)  # Get the actual number of augmentations
        plt.scatter(range(1, num_augmentations + 1), classification_correctness[:num_augmentations], c=classification_correctness[:num_augmentations], cmap='viridis', marker='o')
        plt.title('Severity of Augmentation vs. Classification Correctness')
        plt.xlabel('Augmentation Index (1 to 50)')
        plt.ylabel('Classification Correctness (1 for Correct, 0 for Incorrect)')
        plt.yticks([0, 1], ['Incorrect', 'Correct'])
        plt.grid(True)
        plt.show()


        # Calculate the overall accuracy
        overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100

        print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
        create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))
        break  # Exit the loop after finding and processing an incorrectly classified image
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
C:\Users\jessi\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Incorrectly classified image: n03876231_757.JPEG
Augmentation 1: Predicted label = paintbrush, True label = paintbrush
Augmentation 2: Predicted label = broom, True label = paintbrush
Augmentation 3: Predicted label = paintbrush, True label = paintbrush
Augmentation 4: Predicted label = paintbrush, True label = paintbrush
Augmentation 5: Predicted label = broom, True label = paintbrush
Augmentation 6: Predicted label = broom, True label = paintbrush
Augmentation 7: Predicted label = paintbrush, True label = paintbrush
Augmentation 8: Predicted label = broom, True label = paintbrush
Augmentation 9: Predicted label = paintbrush, True label = paintbrush
Augmentation 10: Predicted label = paintbrush, True label = paintbrush
Augmentation 11: Predicted label = paintbrush, True label = paintbrush
Augmentation 12: Predicted label = paintbrush, True label = paintbrush
Augmentation 13: Predicted label = paintbrush, True label = paintbrush
Augmentation 14: Predicted label = paintbrush, True label = paintbrush
Augmentation 15: Predicted label = paintbrush, True label = paintbrush
Augmentation 16: Predicted label = paintbrush, True label = paintbrush
Augmentation 17: Predicted label = paintbrush, True label = paintbrush
Augmentation 18: Predicted label = paintbrush, True label = paintbrush
Augmentation 19: Predicted label = paintbrush, True label = paintbrush
Augmentation 20: Predicted label = paintbrush, True label = paintbrush
Augmentation 21: Predicted label = broom, True label = paintbrush
Augmentation 22: Predicted label = paintbrush, True label = paintbrush
Augmentation 23: Predicted label = paintbrush, True label = paintbrush
Augmentation 24: Predicted label = paintbrush, True label = paintbrush
Augmentation 25: Predicted label = broom, True label = paintbrush
Augmentation 26: Predicted label = broom, True label = paintbrush
Augmentation 27: Predicted label = broom, True label = paintbrush
Augmentation 28: Predicted label = broom, True label = paintbrush
Augmentation 29: Predicted label = broom, True label = paintbrush
Augmentation 30: Predicted label = broom, True label = paintbrush
Augmentation 31: Predicted label = broom, True label = paintbrush
Augmentation 32: Predicted label = broom, True label = paintbrush
Augmentation 33: Predicted label = broom, True label = paintbrush
Augmentation 34: Predicted label = broom, True label = paintbrush
Augmentation 35: Predicted label = broom, True label = paintbrush
Augmentation 36: Predicted label = broom, True label = paintbrush
Augmentation 37: Predicted label = broom, True label = paintbrush
Augmentation 38: Predicted label = broom, True label = paintbrush
Augmentation 39: Predicted label = broom, True label = paintbrush
Augmentation 40: Predicted label = nail, True label = paintbrush
Augmentation 41: Predicted label = stretcher, True label = paintbrush
Augmentation 42: Predicted label = stretcher, True label = paintbrush
Augmentation 43: Predicted label = stretcher, True label = paintbrush
Augmentation 44: Predicted label = nail, True label = paintbrush
Augmentation 45: Predicted label = stretcher, True label = paintbrush
Augmentation 46: Predicted label = stretcher, True label = paintbrush
Augmentation 47: Predicted label = stretcher, True label = paintbrush
Augmentation 48: Predicted label = stretcher, True label = paintbrush
Augmentation 49: Predicted label = wing, True label = paintbrush
Augmentation 50: Predicted label = stretcher, True label = paintbrush
Overall Accuracy on Augmented Images: 38.00%
No description has been provided for this image
No description has been provided for this image
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[126], line 162
    158 plt.show()
    161 # Calculate the overall accuracy
--> 162 overall_accuracy = sum(classification_correctness) / len(classification_correctness) * 100
    164 print(f"Overall Accuracy on Augmented Images: {overall_accuracy:.2f}%")
    165 create_and_display_montage(augmented_images, predicted_labels, montage_shape=(10, 5))

ZeroDivisionError: division by zero
In [ ]: